package com.config;

import com.zhima.dto.ZhimaOjResult;
import com.zhima.exception.NotAuthorizationException;
import com.zhima.exception.ResourceNotAllowException;
import com.zhima.exception.ResourceNotFoundException;
import com.zhima.exception.ZhimaOjException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : 芝麻
 * @date : 2023-08-27 09:47
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(NotAuthorizationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ZhimaOjResult handleUnAuthorize(NotAuthorizationException e) {
        e.printStackTrace();
        return ZhimaOjResult.fail(e.getMessage());
    }

    @ExceptionHandler(ResourceNotAllowException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ZhimaOjResult handleResourceNotAllowException(ResourceNotAllowException e) {
        e.printStackTrace();
        return ZhimaOjResult.fail(e.getMessage());
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ZhimaOjResult handleResourceNotFoundException(ResourceNotFoundException e) {
        e.printStackTrace();
        return ZhimaOjResult.fail(e.getMessage());
    }

    @ExceptionHandler(ZhimaOjException.class)
    @ResponseStatus(HttpStatus.OK)
    public ZhimaOjResult handleZhimaOjException(ZhimaOjException e) {
        e.printStackTrace();
        return ZhimaOjResult.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ZhimaOjResult handleException(Exception e) {
        e.printStackTrace();
        return ZhimaOjResult.fail(e.getMessage());
    }
}
